import { Injectable } from "@angular/core";
import { BaseHttpService } from "../../../shared/services/baseHttp.service";
import { HttpParams, HttpClient } from "@angular/common/http";
import { SustainableProductsFilterModel } from "./models/sustainableProductsFilter.model";
import { Observable } from "rxjs/Observable";

export enum TargetType {
    ProductInvolvement = 0,
    SustainableProducts = 1
}

@Injectable()
export class ResearchSustainableProductsService extends BaseHttpService {
    constructor(private http: HttpClient) {
        super();
    }

    public getFilters(target: TargetType): Observable<SustainableProductsFilterModel[]> {
        let params: HttpParams = new HttpParams();
        params = params.set("target", TargetType[target])
            .set("withExtendedData", true.toString());

        return this.http.get<SustainableProductsFilterModel[]>(this.serverUrl + '/sustainableproductsserv/api/v1/filters', { params: params });
    }

    public getCompanies(filters: SustainableProductsFilterModel, skip: number, take: number, orderBy: string, orderByAsc: boolean): any {
        let params: HttpParams = new HttpParams();
        params = params.set("skip", skip.toString())
            .set("take", take.toString())
            .set('getCount', 'false')
            .set('orderBy', orderBy)
            .set('orderAscending', orderByAsc.toString());
        params = this.urlEncodeObject(filters, params, 'companyName');
        //because of the HppClient issue with '+' and probably other special chars, we set the parameter into the url
        return this.http.get<any[]>(this.serverUrl + 'sustainableproductsserv/api/v1/companies?companyName=' + this.paramEncode(filters, 'companyName'), { params: params });
    }

    public getCompaniesCount(filters: SustainableProductsFilterModel): Observable<any> {

        let params: HttpParams = new HttpParams();
        params = params.set("getCount", 'true');
        params = this.urlEncodeObject(filters, params);
        //because of the HppClient issue with '+' and probably other special chars, we set the parameter into the url
        return this.http.get<any>(this.serverUrl + 'sustainableproductsserv/api/v1/companies?companyName=' + this.paramEncode(filters, 'companyName'), { params: params });
    }

    public getIndicatorsList(): any {
        let params: HttpParams = new HttpParams();
        params = params.set("getIndicators", 'true');
        return this.http.get<any[]>(this.serverUrl + 'sustainableproductsserv/api/v1/companyassessments', { params: params });
    }

    public getIndustriesList(): any {
        let params: HttpParams = new HttpParams();
        params = params.set("getIndustries", 'true');
        return this.http.get<any[]>(this.serverUrl + 'sustainableproductsserv/api/v1/companyassessments', { params: params });
    }
}